$(function () {
  function reportingTimeDatepickerFn(reportingTimeStart,reportingTimeEnd){
    $('#inspectionRecordsModal .reportingTimeStart').datepicker({
      language: "zh-CN",
      autoclose: true,
      startView: 0,
      format: "yyyy-mm-dd",
      showSecond: true, //显示秒
      timeFormat: 'HH:mm:ss', //格式化时间
      stepHour: 1, //设置步长
      stepMinute: 1,
      stepSecond: 1,
      clearBtn: false,
      todayBtn: false,
      viewDate: reportingTimeStart
    });

    $('#inspectionRecordsModal .reportingTimeEnd').datepicker({
      language: "zh-CN",
      autoclose: true,
      startView: 0,
      format: "yyyy-mm-dd",
      showSecond: true, //显示秒
      timeFormat: 'HH:mm:ss', //格式化时间
      stepHour: 1, //设置步长
      stepMinute: 1,
      stepSecond: 1,
      clearBtn: false,
      todayBtn: false,
      viewDate: reportingTimeEnd
    });
  }
  var n;
  // 年月选择器开启
  $('#search_begindate').click(function () {
    $("#search_begindate").simpleCanleder();
  });
  // 巡检记录 页面打开
  $(".inspectionRecordsBtn").on("click", function (event) {
    event.preventDefault();
    $(".inspectionRecords-pop").addClass("is-visible3");
    $('#inspectionRecordsModal .taskState').val('');
    $('#inspectionRecordsModal .level').val('');
    var reportingTimeStart = moment().format('YYYY-MM') + '-01';
    var reportingTimeEnd = moment().format('YYYY-MM-DD');
    $('#inspectionRecordsModal .reportingTimeStart').val(reportingTimeStart);
    $('#inspectionRecordsModal .reportingTimeEnd').val(reportingTimeEnd);
    reportingTimeDatepickerFn(reportingTimeStart,reportingTimeEnd);
    n = 1;
    inspectionRecordsPageList(n);
  });

  // 巡检记录 页面 关闭
  $(".inspectionRecords-pop").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close") ||
      $(event.target).is(".inspectionRecords-pop")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
      $("#inspectionRecordsModal").find("input").val('');
      $('#inspectionRecordsModal select').find("option:first").prop("selected", "selected");
    }
  });

  // 巡检记录 分页
  var inspectionRecordsPageNum = 1;
  var inspectionRecordsPageNumCurr = 1;

  function inspectionRecordsListPageFunc(pageSum, startPageNum) {
    if (pageSum <= 1) {
      $('#inspectionRecordsPageBox .pageJump').hide();
    } else {
      $('#inspectionRecordsPageBox .pageJump').show();
    }
    Page({
      num: pageSum, //页码数
      startnum: startPageNum, //指定页码
      elem: $('#inspectionRecordsPage'), //指定的元素
      callback: function (n) { //回调函数
        console.log(n)
        inspectionRecordsPageNumCurr = n;
        inspectionRecordsPageList(n);
      }
    });
  }
  // 点击人员管理中的巡检记录，按人员进行筛选
  $('#personManagementModal .personManagementTableBox').on('click', '.personInspection', function () {
    var name = $(this).attr('name');
    console.log(name);
    var reportingTimeStart = moment().format('YYYY-MM') + '-01';
    var reportingTimeEnd = moment().format('YYYY-MM-DD');
    $('#inspectionRecordsModal .reportingTimeStart').val(reportingTimeStart);
    $('#inspectionRecordsModal .reportingTimeEnd').val(reportingTimeEnd);
    
    reportingTimeDatepickerFn(reportingTimeStart,reportingTimeEnd);
    $('#inspectionRecordsModal .reportPerson').val(name);
    $(".inspectionRecords-pop").addClass("is-visible3");
    inspectionRecordsPageList(1);
  });
  // 点击楼宇管理中的巡检记录，按楼宇进行筛选
  console.log($('#buildingManagementModal .buildingManagementTableBox'))
  $('#buildingManagementModal .buildingManagementTableBox').on('click', '.personInspection', function () {
    var address = $(this).attr('address');
    console.log(address);
    var reportingTimeStart = moment().format('YYYY-MM') + '-01';
    var reportingTimeEnd = moment().format('YYYY-MM-DD');
    $('#inspectionRecordsModal .reportingTimeStart').val(reportingTimeStart);
    $('#inspectionRecordsModal .reportingTimeEnd').val(reportingTimeEnd);
    reportingTimeDatepickerFn(reportingTimeStart,reportingTimeEnd);
    $('#inspectionRecordsModal .inspectionRecordsSearchBox .address').val(address);
    $(".inspectionRecords-pop").addClass("is-visible3");
    // $(".buildingManagement-pop").removeClass("is-visible3");
    inspectionRecordsPageList(1);
  });
  // 渲染 巡检记录列表
  function inspectionRecordsPageList(n) {
    var orgId = localStorage.getItem('userOrgId');

    var reportPerson = $("#inspectionRecordsModal").find(".reportPerson").val();
    var address = $("#inspectionRecordsModal").find(".address").val();
    var reportingTimeStart = $("#inspectionRecordsModal").find(".reportingTimeStart").val();
    var reportingTimeEnd = $("#inspectionRecordsModal").find(".reportingTimeEnd").val();
    var level = $("#inspectionRecordsModal .level").find("option:selected").val();
    var taskState = $("#inspectionRecordsModal .taskState").find("option:selected").val();

    var url = '/building/getBuildingInspectionRecord?';
    if (!reportPerson) {
      url = url;
    } else {
      url = url + 'reportPerson=' + reportPerson + '&';
    }
    if (!address) {
      url = url;
    } else {
      url = url + 'address=' + address + '&';
    }
    if (!reportingTimeStart) {
      url = url;
    } else {
      url = url + 'startTime=' + reportingTimeStart + '&';
    }
    if (!reportingTimeEnd) {
      url = url;
    } else {
      url = url + 'endTime=' + reportingTimeEnd + '&';
    }
    if (!level) {
      url = url;
    } else {
      url = url + 'level=' + level + '&';
    }
    if (!taskState) {
      url = url;
    } else {
      url = url + 'taskState=' + taskState + '&';
    }
    $.ajax({
      // building/getBuildingInspectionRecord?reportPerson=1&communityName=aaa&reportingTime=2018-08-28&level=11&taskState=11&size=1&page=0
      // url: '/building/getBuildingInspectionRecord?size=8&page=' + (n - 1),
      url: url + 'size=8&page=' + (n - 1),
      type: 'GET',
      async: false,
      success: function (result) {
        console.log(result);
        if (result.status == 200) {
          $('#inspectionRecordsPageBox').show();
          var content = result.data.content;
          inspectionRecordsPageNum = result.data.totalPages;
          size = result.data.size;
          $('#inspectionRecordsModal .tbodyList').html('');
          for (var i = 0; i < content.length; i++) {
            // 获取上报时间
            // var reportingTime = content[i].reportingTime;
            // reportingTime = reportingTime.substring(0, 7);
            // 获取任务状态
            var taskState = content[i].taskState;
            var taskStateText = '';
            if (taskState == 0) {
              taskStateText = '驳回';
            } else if (taskState == 1) {
              taskStateText = '处理中';
            } else if (taskState == 2) {
              taskStateText = '结束';
            }
            // 获取级别
            var level = content[i].level;
            var levelText = '';
            var active = '';
            if (level == 0) {
              levelText = '重大';
              active = 'colorff4242';
            } else if (level == 1) {
              levelText = '较大';
              active = 'colorffb142';
            } else if (level == 2) {
              levelText = '一般';
              active = 'color61ff42';
            } else if (level == 3) {
              levelText = '正常';
              active = 'color61ff42';
            }
            var reportingTime = content[i].reportingTime;
            if (reportingTime) {
              reportingTime = moment(reportingTime).format('YYYY-MM-DD HH:mm');
            } else {
              reportingTime = '空';
            }
            var onlyValue = content[i].onlyValue;
            // $('#inspectionRecordsModal .tbodyList').append('<tr><td>' + ((n - 1) * size + i + 1) + '</td><td>' + content[i].address + '</td><td>' + content[i].photoNumber + '</td><td>' + content[i].reportPerson + '</td><td>' + content[i].node + '</td><td>' + taskStateText + '</td><td>' + content[i].hiddenDanger + '</td><td>' + levelText + '</td><td>' + reportingTime + '</td><td><span class="seeDetailsStyle inspectionDetails-target" onlyValue="'+onlyValue+'" uuid="' + content[i].uuid + '" buildingName="' + content[i].buildingName + '" buildingId="' + content[i].id + '">查看详情</span></td></tr>');
            $('#inspectionRecordsModal .tbodyList').append('<tr><td>' + ((n - 1) * size + i + 1) + '</td><td>' + content[i].address + '</td><td>' + content[i].photoNumber + '</td><td>' + content[i].reportPerson + '</td><td>' + content[i].node + '</td><td>' + content[i].hiddenDanger + '</td><td class="' + active + '">' + levelText + '</td><td>' + reportingTime + '</td><td><span class="seeDetailsStyle inspectionDetails-target" onlyValue="' + onlyValue + '" uuid="' + content[i].uuid + '" buildingName="' + content[i].buildingName + '" buildingId="' + content[i].id + '">查看详情</span></td></tr>');
          }
          inspectionRecordsListPageFunc(inspectionRecordsPageNum, n);
        } else {
          $('#inspectionRecordsPageBox').hide();
        }

      },
      error: function (res) {
        $('#inspectionRecordsPageBox').hide();
      }
    });
  }
  var inspectionRecordsSearchFn = _.debounce(function () {
    // 获取输入内容
    var reportPerson = $("#inspectionRecordsModal").find(".reportPerson").val();
    var address = $("#inspectionRecordsModal").find(".address").val();
    var reportingTimeStart = $("#inspectionRecordsModal").find(".reportingTimeStart").val();
    var reportingTimeEnd = $("#inspectionRecordsModal").find(".reportingTimeEnd").val();
    var level = $("#inspectionRecordsModal .level").find("option:selected").val();
    var taskState = $("#inspectionRecordsModal .taskState").find("option:selected").val();

    var url = '/building/getBuildingInspectionRecord?';
    if (!reportPerson) {
      url = url;
    } else {
      url = url + 'reportPerson=' + reportPerson + '&';
    }
    if (!address) {
      url = url;
    } else {
      url = url + 'address=' + address + '&';
    }
    if (!reportingTimeStart) {
      url = url;
    } else {
      url = url + 'startTime=' + reportingTimeStart + '&';
    }
    if (!reportingTimeEnd) {
      url = url;
    } else {
      url = url + 'endTime=' + reportingTimeEnd + '&';
    }
    if (!level) {
      url = url;
    } else {
      url = url + 'level=' + level + '&';
    }
    if (!taskState) {
      url = url;
    } else {
      url = url + 'taskState=' + taskState + '&';
    }
    $.ajax({
      // http://localhost:9092/building/getBuildingInspectionRecord?level=1&taskState=1&size=8&page=0
      // http://localhost:9092/building/getBuildingInspectionRecord?reportPerson=1&communityName=aaa&reportingTime=2018-08-28&level=11&taskState=11&size=1&page=0
      // url: url + 'size=8&page=' + (n - 1),
      url: url + 'size=8&page=0',
      type: 'GET',
      async: false,
      success: function (result) {
        console.log(result);
        var content = result.data.content;
        inspectionRecordsPageNum = result.data.totalPages;
        size = result.data.size;
        $('#inspectionRecordsModal .tbodyList').html('');
        for (var i = 0; i < content.length; i++) {
          // 获取任务状态
          var taskState = content[i].taskState;
          var taskStateText = '';
          if (taskState == 0) {
            taskStateText = '驳回';
          } else if (taskState == 1) {
            taskStateText = '处理中';
          } else if (taskState == 2) {
            taskStateText = '结束';
          }
          // 获取级别
          var level = content[i].level;
          var levelText = '';
          if (level == 0) {
            levelText = '重大';
            active = 'colorff4242';
          } else if (level == 1) {
            levelText = '较大';
            active = 'colorffb142';
          } else if (level == 2) {
            levelText = '一般';
            active = 'color61ff42';
          } else if (level == 3) {
            levelText = '正常';
            active = 'color61ff42';
          }
          var reportingTime = content[i].reportingTime ? moment(content[i].reportingTime).format('YYYY-MM-DD HH:mm') : '空';
          var onlyValue = content[i].onlyValue;
          $('#inspectionRecordsModal .tbodyList').append('<tr><td>' + (+i + 1) + '</td><td>' + content[i].address + '</td><td>' + content[i].photoNumber + '</td><td>' + content[i].reportPerson + '</td><td>' + content[i].node + '</td><td>' + content[i].hiddenDanger + '</td><td class="' + active + '">' + levelText + '</td><td>' + reportingTime + '</td><td><span class="seeDetailsStyle inspectionDetails-target" onlyValue="' + onlyValue + '" uuid="' + content[i].uuid + '">查看详情</span></td></tr>');

          // $('#inspectionRecordsModal .tbodyList').append('<tr><td>' + ( + i + 1) + '</td><td>' + content[i].address + '</td><td>' + content[i].photoNumber + '</td><td>' + content[i].reportPerson + '</td><td>' + content[i].node + '</td><td>' + taskStateText + '</td><td>' + content[i].hiddenDanger + '</td><td>' + levelText + '</td><td>' + reportingTime + '</td><td><span class="seeDetailsStyle inspectionDetails-target" onlyValue="'+onlyValue+'" uuid="' + content[i].uuid + '">查看详情</span></td></tr>');
        }
        inspectionRecordsListPageFunc(inspectionRecordsPageNum, n);
      }
    });
  }, 500);
  // 搜索按钮 查询事件
  $("#inspectionRecordsSearchBtn").on("click", function () {
    inspectionRecordsSearchFn();
  });
  // 小区名称模糊搜索 
  var communityNameSearchContent = '';
  var reportPersonSearchContent = '';
  $('body').on('click', function () {
    $("#inspectionRecordsModal .communityNameSearch").hide();
    $("#inspectionRecordsModal .reportPersonSearch").hide();
  });
  communityNameSearchFunc();

  function communityNameSearchFunc() {
    var fn = _.debounce(function () {
      $("#inspectionRecordsModal .communityNameSearch").show();

      if ($("#inspectionRecordsModal .communityNameSearch li")) { //键盘抬起后就判断页面中是否存在这个元素,存在就删除
        $("#inspectionRecordsModal .communityNameSearch li").remove();
      }
      communityNameSearchContent = $("#inspectionRecordsModal .address").val();
      communityNameSearchContent = $.trim(communityNameSearchContent);
      console.log(communityNameSearchContent)
      //  http://localhost:9092/building/getBuildings?address=%E5%87%A4%E5%87%B0%E4%B8%AD%E5%BF%83&size=10&page=0
      if (communityNameSearchContent) {
        $.ajax({
          url: "/building/getBuildings?address=" + communityNameSearchContent + "&size=1000&page=0",
          type: "GET",
          async: false,
          success: function (result) {
            console.log(result)
            var data = result.data.content;
            for (var i = 0; i < data.length; i++) {
              $("#inspectionRecordsModal .communityNameSearch").append('<li>' + data[i].address + '</li>');
            }
          }
        });
        $("#inspectionRecordsModal .communityNameSearch").show();
      }

    }, 500);
    //模糊查询
    $("#inspectionRecordsModal .address").on("input", function (e) {
      fn();

    });
    //直接输入地址,点击列表查询
    $("#inspectionRecordsModal .communityNameSearch").on("click", "li", function () {
      $("#inspectionRecordsModal .communityNameSearch").show();
      var liVal = $(this).text();
      liVal = $.trim(liVal);
      var searchVal = $("#inspectionRecordsModal .address").val(liVal);
      communityNameSearchContent = liVal;
      $("#inspectionRecordsModal .communityNameSearch").html(""); //hide
      return false;
    });
  }

  // 负责人模糊搜索 
  // reportPersonSearchFunc();
  function reportPersonSearchFunc() {
    var fn = _.debounce(function () {
      $("#inspectionRecordsModal .reportPersonSearch").show();

      if ($("#inspectionRecordsModal .reportPersonSearch li")) { //键盘抬起后就判断页面中是否存在这个元素,存在就删除
        $("#inspectionRecordsModal .reportPersonSearch li").remove();
      }
      reportPersonSearchContent = $("#inspectionRecordsModal .reportPerson").val();
      reportPersonSearchContent = $.trim(reportPersonSearchContent);
      console.log(reportPersonSearchContent)
      //  /building/getBuildingInspectionRecord?reportPerson=凤凰&size=8&page=0
      if (reportPersonSearchContent) {

        $.ajax({
          url: "/building/getBuildingInspectionRecord?reportPerson=" + reportPersonSearchContent + "&size=1000&page=0",
          type: "GET",
          async: false,
          success: function (result) {
            console.log(result)
            var data = result.data.content;
            for (var i = 0; i < data.length; i++) {
              $("#inspectionRecordsModal .reportPersonSearch").append('<li>' + data[i].reportPerson + '</li>');
            }
          }
        });
        $("#inspectionRecordsModal .reportPersonSearch").show();
      }

    }, 500);
    //模糊查询
    $("#inspectionRecordsModal .reportPerson").on("input", function (e) {
      fn();

    });
    //直接输入地址,点击列表查询
    $("#inspectionRecordsModal .reportPersonSearch").on("click", "li", function () {
      $("#inspectionRecordsModal .reportPersonSearch").show();
      var liVal = $(this).text();
      liVal = $.trim(liVal);
      var searchVal = $("#inspectionRecordsModal .reportPerson").val(liVal);
      reportPersonSearchContent = liVal;
      $("#inspectionRecordsModal .reportPersonSearch").html(""); //hide
      return false;
    });
  }

});